//
//  ZJOSTeachersViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/7/20.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJOSTeachersViewController.h"
#import "ZJOSTeacherView.h"
#import "ZJCommunitySchoolModel.h"
#import "ZJOSTeacherInfoViewController.h"
@interface ZJOSTeachersViewController ()
{
    ZJCommunitySchoolModel *_model;/**< 党校信息 */
}
/** scrollView */
@property (nonatomic, strong) UIScrollView *scrollView;
/** 数据 */
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation ZJOSTeachersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.height = self.view.height - LP_TopHeight - 44 - 268 * [UIScreen zj_screenHScale];
    [self.view addSubview:self.scrollView];
    _model = [ZJCommunitySchoolModel yy_modelWithJSON:self.dataDic];
    [self netWorking];
 
}

- (void)netWorking{
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"PB_PARTYSCHOOL_ID":_model.ID};
    [NetWorkingManager postWithUrlString:[NetWorkingURL getSchoolMsg] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//请求成功
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[ZJSchoolTeacherModel class] json:[object objectForKey:@"TEACHER_LIST"]] mutableCopy];
            [weakSelf updateScrollView];
        }
    } Fail:^(id object) {
        
    }];
    
}


- (void)updateScrollView{
    for (UIView *view in self.scrollView.subviews) {
        if ([view isKindOfClass:[ZJOSTeacherView class]]) {
            [view removeFromSuperview];
        }
    }
    
    CGFloat viewWidth = 96 * [UIScreen zj_screenWScale];
    CGFloat viewHeight = 165 * [UIScreen zj_screenHScale];
    CGFloat spaceW =(self.view.width - viewWidth * 3 - 24 * [UIScreen zj_screenWScale]) / 2;
    CGFloat spaceH = 16 * [UIScreen zj_screenHScale];
    
    for (int i = 0; i < self.dataArray.count; i ++) {
        NSInteger yu = i % 3;
        NSInteger zeng = i / 3;
        
        ZJOSTeacherView *view = [[ZJOSTeacherView alloc]initWithFrame:CGRectMake(12 * [UIScreen zj_screenWScale] + yu * (viewWidth + spaceW), spaceH + zeng * (spaceH + viewHeight), viewWidth, viewHeight)];
        view.tag = i;
        view.model = self.dataArray[i];
        UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_click:)];
        [view addGestureRecognizer:tapG];
        [self.scrollView addSubview:view];
        
        self.scrollView.contentSize = CGSizeMake(self.view.width, view.bottom);
    }
    
}


#pragma mark - 点击事v件
- (void)action_click:(UITapGestureRecognizer *)sender{
    ZJOSTeacherInfoViewController *vc = [ZJOSTeacherInfoViewController new];
    vc.model = self.dataArray[sender.view.tag];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - Setter


#pragma mark - Getters
- (UIScrollView *)scrollView{
    if (_scrollView == nil){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
        _scrollView.contentSize = _scrollView.size;
    }
    return _scrollView;
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
@end
